{extend name="common/base" /}
{block name="body"} 
<!-- ElementUI -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
<div class="Content" id="content">
    <div id="vue-app" class="container">
        <el-form ref="form" :model="data"  :rules="rules" label-width="100px">
            <el-tabs v-model="activeName">
                <el-tab-pane label="基础信息" name="first">
                    <el-form-item label="商品类型" prop="type">
                        <el-radio v-model="data.goods_type"   @change="onGoodsTypeChange" :label="1">实物</el-radio>
                        <el-radio v-model="data.goods_type"  @change="onGoodsTypeChange" :label="2">数字珍品</el-radio>
                    </el-form-item>  
                    <el-form-item label="分类" prop="cate_id" v-if="data.goods_type == 1">
                        <el-select v-model.trim="data.cate_id" placeholder="请选择分类" filterable clearable  class="filter-item" >
                              <el-option  :label="item.html + item.cate_name"  :value="item.id" v-for="(item,idx) in category" :key="idx" ></el-option>
                          </el-select>
                    </el-form-item>
                        <el-form-item label="商品名称" prop="store_name">
                            <el-input v-model.trim="data.store_name" placeholder="请填写商品名称" />
                        </el-form-item>
                        <el-form-item label="单位" prop="unit_name">
                            <el-input v-model.trim="data.unit_name" placeholder="请填写单位" />
                    </el-form-item>
                    <el-form-item label="限购数量" prop="buy_limit">
                                <el-input-number :min="0" v-model.trim="data.buy_limit" placeholder="限购数量" />
                                <p>0或者小于0表示不限购</p>
                        </el-form-item>
                    <el-form-item label="艺术家" prop="artist_id">
                        <el-select v-model.trim="data.artist_id" placeholder="请选择艺术家" filterable clearable   >
                            <el-option  :label="item.name"  :value="item.id" v-for="(item,idx) in artist_list" :key="idx" ></el-option>
                        </el-select>
                    </el-form-item>
                        <el-form-item label="简介" prop="store_info">
                            <el-input type="textarea" v-model.trim="data.store_info" placeholder="请填写简介"  rows="6" />
                    </el-form-item>
                    <el-form-item label="缩略图" prop="image" >
                        <el-upload
                            action="/merchant/upload_img"
                            :limit=1
                            list-type="picture-card"
                            :file-list="data.imagefileList"
                            accept="image/png, image/jpeg, image/gif, image/jpg"
                            :on-success="handleimageSuccess"
                            :before-upload="beforeimageUpload"
                            :on-exceed="handleimageExceed"
                            :on-preview="handleimagePreview"
                            :on-remove="handleimageRemove">
                            <i class="el-icon-plus"></i>
                            </el-upload>
                    </el-form-item>
                    <el-form-item label="轮播图" prop="slider_image">
                        <el-upload
                            action="/merchant/upload_img"
                            multiple
                            list-type="picture-card"
                            :on-preview="handleslider_imagePreview"
                            :file-list="data.slider_imagefileList"
                            accept="image/png, image/jpeg, image/gif, image/jpg"
                            :on-success="handleslider_imageSuccess"
                            :on-remove="handleslider_imageRemove"
                            >
                            <i class="el-icon-plus"></i>
                            </el-upload>
                    </el-form-item>
                    <el-form-item  label="商品规格" prop="spec_type">
                        <el-radio v-model="data.spec_type" :label="0">单规格</el-radio>
                        <el-radio v-model="data.spec_type" v-if="data.goods_type == 1" :label="1">多规格</el-radio>
                    </el-form-item>  
                    <el-form-item v-if="data.spec_type == 0" >
                        <el-table
                                border
                                :data="data.attr"
                                style="width: 100%">
                                <el-table-column  label="图片"  width="120"  header-align="center" style="text-align:center">
                                        <template slot-scope="scope">
                                            <el-upload
                                            style="text-align:center"
                                            class="avatar-uploader"
                                            action="/merchant/upload_img"
                                            :show-file-list="false"
                                            accept="image/png, image/jpeg, image/gif, image/jpg"
                                            :on-success="handlepicSuccess">
                                            <img v-if="scope.row.pic" :src="scope.row.pic" class="avatar" style="width:60px;height:60px">
                                            <i v-else class="el-icon-plus avatar-uploader-icon" style="width:60px;height:60px;line-height:60px"></i>
                                            </el-upload>
                                        </template>
                                </el-table-column>
                                <el-table-column
                                header-align="center"
                                    label="售价"
                                    >
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.price" clearable placeholder="请输入商品售价"></el-input>
                                    </template>
                                </el-table-column>

                                <el-table-column
                                header-align="center"
                                    label="原价"
                                    >
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.ot_price" clearable placeholder="请输入商品原价"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                header-align="center"
                                    label="库存"
                                    >
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.stock" clearable placeholder="请输入库存"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                header-align="center"
                                    label="产品编号"
                                    >
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.bar_code" clearable placeholder="请输入产品编号"></el-input>
                                    </template>
                                </el-table-column>
                                </el-table>
                        </el-form-item> 
                        <el-form-item label="规则模板" v-if="data.spec_type == 1">
                            <el-select  placeholder="请选择模板" v-model="ruleIndex" clearable  class="filter-item" >
                                <el-option  :label="item.rule_name" :value="idx" v-for="(item,idx) in rule_list" :key="idx" ></el-option>
                            </el-select>
                            <el-button type="primary" style="margin-left:10px" @click="allRule">确认</el-button>
                        </el-form-item>   
                        <div v-if="data.spec_type == 1">
                            <div v-for="(item,index) in data.items" :key="index"  >
                                  <el-tag closable  type="success" style="margin-bottom:10px;margin-left:120px;"  @close="deleteItem(index)" >{{item.value}}</el-tag>
                                  <div style="display:flex;align-items:center;margin-left:120px;">
                                      <el-tag closable style="margin-bottom:10px;margin-right:5px;" size="small" v-for="(val,inx) in item.detail" :key="inx" @close="deleteValue(item,inx)" >{{val}}
                                      </el-tag>
                                      <el-input v-model="item.detailValue" size="mini" style="width:120px;margin-bottom:10px" clearable placeholder="请输入"></el-input>
                                        <el-button type="text"  @click="addDetail(item)" style="margin-bottom:10px;margin-left:22px">添加</el-button>
                                  </div>
                            </div>   
                        </div>
                        <div style="display:flex;align-items:center" v-if="newRule">
                            <el-form-item label="规格">
                                <el-input  v-model="formDynamic.attrsName" placeholder="请输入规格" clearable></el-input>
                            </el-form-item>
                            <el-form-item label="规格值：">
                                <el-input v-model="formDynamic.attrsVal" placeholder="请输入规格值" clearable></el-input>
                            </el-form-item>
                            <el-button type="text"  @click="createAttrName" style="margin-bottom:22px;margin-left:22px">添加</el-button>
                            <el-button type="text" @click="newRule = false"  style="margin-bottom:22px">取消</el-button>
                        </div>
                         
                        <div style="display:flex;align-items:center;margin-left:120px" v-if="newRule == false  && ruleBool == true && data.spec_type == 1">
                            <el-button type="primary"  size="small"  @click="newRule = true" style="margin-bottom:22px;margin-left:22px">添加新规格</el-button>
                            <el-button type="success"  size="small" @click="generates"  style="margin-bottom:22px">立即生成</el-button>
                        </div>  
                        <el-form-item :label="'批量设置'" v-if="data.attrs.length && formHeader.length && data.spec_type == 1" >
                            <el-table
                              border
                              :data="batchAttr"
                              style="width: 100%">
                              <el-table-column  label="图片"  width="120"  header-align="center" style="text-align:center">
                                    <template slot-scope="scope">
                                        <el-upload
                                        style="text-align:center"
                                          class="avatar-uploader"
                                        action="/merchant/upload_img"
                                        :show-file-list="false"
                                        accept="image/png, image/jpeg, image/gif, image/jpg"
                                        :on-success="handleBatchpicSuccess">
                                          <img v-if="scope.row.pic" :src="scope.row.pic" class="avatar" style="width:60px;height:60px">
                                          <i v-else class="el-icon-plus avatar-uploader-icon" style="width:60px;height:60px;line-height:60px"></i>
                                        </el-upload>
                                    </template>
                              </el-table-column>
                              <el-table-column
                              header-align="center"
                                label="售价"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.price" clearable placeholder="请输入商品售价"></el-input>
                                </template>
                              </el-table-column>
                              <!-- <el-table-column
                              header-align="center"
                                label="成本价"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.cost"  clearable placeholder="请输入商品成本价"></el-input>
                                </template>
                              </el-table-column> -->
                              <el-table-column
                              header-align="center"
                                label="原价"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.ot_price" clearable placeholder="请输入商品原价"></el-input>
                                </template>
                              </el-table-column>
                              <el-table-column
                              header-align="center"
                                label="库存"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.stock" clearable placeholder="请输入库存"></el-input>
                                </template>
                              </el-table-column>
                              <el-table-column
                              header-align="center"
                                label="产品编号"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.bar_code" clearable placeholder="请输入产品编号"></el-input>
                                </template>
                              </el-table-column>
                              <el-table-column align="center" width="250" :label="'操作'">
                                <template>
                                    <el-button v-waves   type="primary"  size='mini' icon="el-icon-edit" @click="batchAdd">
                                        {{ "批量修改" }}
                                    </el-button>
                                    <el-button v-waves  type="danger" size='mini' icon="el-icon-delete"   @click="batchClear">
                                        {{ "清空" }}
                                    </el-button>
                                </template>
                                </el-table-column>
                            </el-table>
                      </el-form-item> 
                        <el-form-item :label="'商品属性'" v-if="data.attrs.length && formHeader.length && data.spec_type == 1">
                            <el-table
                              border
                              :data="this.data.attrs"
                              style="width: 100%">
        
                                <el-table-column v-for="(n,v) in data.header" :key="v" :prop="n.value" :label="n.value" header-align="center">
                                    <template   scope="scope">
                                        <p style="text-align:center">{{scope.row.detail[n.value]}}</p>
                                    </template>
                                </el-table-column>
              
                              <el-table-column  label="图片"  width="120"  header-align="center" style="text-align:center">
                                    <template slot-scope="scope">
                                        <el-upload
                                        style="text-align:center"
                                          class="avatar-uploader"
                                        action="/merchant/upload_img"

                                        :show-file-list="false"
                                        :on-success="
                                              (response, file, fileList) =>
                                                handleItemSuccess(
                                                  response,
                                                  file,
                                                  fileList,
                                                  scope.$index)"
                                        accept="image/png, image/jpeg, image/gif, image/jpg"
                                        >
                                          <img v-if="scope.row.pic" :src="scope.row.pic" class="avatar" style="width:60px;height:60px">
                                          <i v-else class="el-icon-plus avatar-uploader-icon" style="width:60px;height:60px;line-height:60px"></i>
                                        </el-upload>
                                    </template>
                              </el-table-column>
                              <el-table-column
                              header-align="center"
                                label="售价"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.price" clearable placeholder="请输入商品售价"></el-input>
                                </template>
                              </el-table-column>
                              <!-- <el-table-column
                              header-align="center"
                                label="成本价"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.cost"  clearable placeholder="请输入商品成本价"></el-input>
                                </template>
                              </el-table-column> -->
                              <el-table-column
                              header-align="center"
                                label="原价"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.ot_price" clearable placeholder="请输入商品原价"></el-input>
                                </template>
                              </el-table-column>
                              <el-table-column
                              header-align="center"
                                label="库存"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.stock" clearable placeholder="请输入库存"></el-input>
                                </template>
                              </el-table-column>
                              <el-table-column
                              header-align="center"
                                label="产品编号"
                                >
                                <template slot-scope="scope">
                                      <el-input v-model="scope.row.bar_code" clearable placeholder="请输入产品编号"></el-input>
                                </template>
                              </el-table-column>
                              <el-table-column align="center" width="250" :label="'操作'">
                                <template slot-scope="scope">
                                    <el-button v-waves  type="danger" size='mini' icon="el-icon-delete"   @click="deleteAttrs(scope.$index)">
                                        {{ "删除" }}
                                    </el-button>
                                </template>
                                </el-table-column>
                            </el-table>
                      </el-form-item>  
                </el-tab-pane>
                <el-tab-pane label="商品详情" name="second">
                    <el-form-item label="详情" prop="description">
                        <div id="editor—wrapper" style="border: 1px solid #ccc;">
                            <div id="editor-toolbar" style="border-bottom: 1px solid #ccc;"><!-- 工具栏 --></div>
                            <div id="editor-container" style="min-height: 400px;"><!-- 编辑器 --></div>
                          </div>
                    </el-form-item>
                </el-tab-pane>
                <el-tab-pane label="其他设置" name="third">
                    <el-form-item label="运费模板" prop="template_id">
                      <el-select v-model.trim="data.template_id" placeholder="请选择运费模板" filterable clearable   >
                          <el-option  :label="'无需配送'"  :value="0"></el-option>
                          <el-option  :label="item.name"  :value="item.id" v-for="(item,idx) in template_list" :key="idx" ></el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="上架">
                      <el-switch v-model="data.is_show" :active-value="1" :inactive-value="0"></el-switch>
                    </el-form-item>
                    <el-form-item label="热卖">
                      <el-switch v-model="data.is_hot" :active-value="1" :inactive-value="0"></el-switch>
                    </el-form-item>
                     <el-form-item label="新品">
                      <el-switch v-model="data.is_new" :active-value="1" :inactive-value="0"></el-switch>
                    </el-form-item>
                    <el-form-item label="排序">
                      <el-input-number v-model="data.sort"  :min="1" label="排序"></el-input-number>
                    </el-form-item>
                </el-tab-pane>
            </el-tabs>
            <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
                <img width="100%" :src="previewUrl" alt="">
            </el-dialog> 
            <el-form-item>
                <el-button type="primary"  v-if="activeName !='first'" @click="back">上一步</el-button>
                <el-button type="primary"  v-if="activeName !='third'" @click="next">下一步</el-button>
                <el-button type="primary"  v-if="activeName=='third'" @click="handleSubmit">提交</el-button>
            </el-form-item> 
        </el-form>
    </div>
</div>
{/block}
{block name="js"} 
<script src="//unpkg.com/jquery@3.6.0/dist/jquery.min.js"></script> 
<script src="https://unpkg.com/vue@2.7.16/dist/vue.min.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
<script>
    $(document).ready(function() {
        const E = window.wangEditor

        window.editor = E.createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: {
            placeholder: 'Type here...',
            MENU_CONF: {
            uploadImage: {
                fieldName: 'your-fileName',
                base64LimitSize: 10 * 1024 * 1024 // 10M 以下插入 base64
            }
            },
            onChange(editor) {
                
            }
        }
        })

        window.toolbar = E.createToolbar({
            editor,
            selector: '#editor-toolbar',
            config: {}
        })
    });
 
  </script>
<script>
    var vueApp = new Vue({
        el: '#vue-app',
        data: {
            rules: {
            
            },
            category:[],
            template_list:[],
            rule_list:[],
            artist_list:[],
            ruleIndex: '',
            newRule: false,//是否添加新规则
            formHeader:[],
            activeName: 'first',
            loading:false,
            dialogVisible:false,
            ruleBool:false,
            goods_type : 1,//商品类型 1实物 2数字珍品
            previewUrl:'',
            batchAttr:[
                    {
                    pic: '',
                    price: 0,
                    cost: 0,
                    ot_price: 0,
                    stock: 0,
                    bar_code: ''
                }
            ],
            formDynamic: {
                attrsName: '',
                attrsVal: ''
            },
             useEditor: true,
            editorContent: '',
            data: {
                    header: [],
                    image:[],
                    template_id:0,
                    imagepreviewurl:'',
                    imagefileList:[],
                    slider_image:[],
                    buy_limit:0,
                    slider_imagepreviewurl:'',
                    slider_imagefileList:[],
                    store_name:'',
                    attr: [
                        {
                        pic: '',
                        price: 0,
                        cost: 0,
                        ot_price: 0,
                        stock: 0,
                        bar_code: ''
                    },
                    ],
                    items:[],
                    attrs: [],
                    spec_type:0,
                    store_info:'',
                    keyword:'',
                    cate_id:'',
                    goods_type : 1, //商品类型 1实物 2数字珍品
                    unit_name:'',
                    sort:'',
                    sales:'',
                    stock:'',
                    is_show:1,
                    is_hot:0,
                    is_new:1,
                    is_postage:true,
                    cost:'',
                    browse:'',
                    video_link:'',
                    video_linkfileList:[],
                    video_linkpercentage:0,
                    description:'',
                    artist_id:''

            }
            
        },
        mounted() {           // 生命周期钩子
            this.init();
        },
        methods: {
      
            init(){
                axios.get('/merchant/goods_add_init')
                    .then(response => {
                        var response_data = response.data
                        if(response_data.code == 1){
                            this.category = response_data.data.category;
                            this.artist_list = response_data.data.artist_list;
                            this.rule_list = response_data.data.rule_list;
                            this.template_list = response_data.data.template_list;
                        }else{
                            // 错误处理
                            this.$message.error('加载失败: ' + response_data.data.msg);
                            console.error('请求错误:', response_data.msg);
                        }
                    })
                    .catch(error => {
                        // 错误处理
                        this.$message.error('加载失败: ' + error.message);
                        console.error('请求错误:', error);
                    })

            },
            
            batchAdd:function(){
                var that = this;
                this.$set(this.data,'attrs',this.data.attrs.map(function (item) {
  
                    if (that.batchAttr[0].pic) {
                        item.pic = that.batchAttr[0].pic;
                    }
                    if (that.batchAttr[0].price > 0){
                        item.price = that.batchAttr[0].price;
                    }
                  
                    if (that.batchAttr[0].cost > 0){
                        item.cost = that.batchAttr[0].cost;
                    }
                    if (that.batchAttr[0].ot_price > 0){
                        item.ot_price = that.batchAttr[0].ot_price;
                    }
                    if (that.batchAttr[0].stock > 0){
                        item.stock = that.batchAttr[0].stock;
                    }
                    if (that.batchAttr[0].bar_code != ''){
                        item.bar_code = that.batchAttr[0].bar_code;
                    }

                    return item;
                }));
            },
            batchClear:function(){
                this.$set(this,'batchAttr',{
                    pic: '',
                    price: 0,
                    cost: 0,
                    ot_price: 0,
                    stock: 0,
                    bar_code: '',
                    weight: 0,
                    volume: 0,
                });
            },
            allRule:function () {
                if (this.ruleIndex !== '') {
                    var rule = this.rule_list[this.ruleIndex];
                    if (rule) {
                        this.ruleBool = true;
                        var rule_value = rule.rule_value.map(function (item) {
                            return item;
                        });
                        this.$set(this.data,'items',rule_value);
                        this.$set(this.data,'attrs',[]);
                        this.$set(this,'formHeader',[]);
                        return true;
                    }
                   
                }
                
               return this.$message.error('选择的属性无效');
            },
            deleteAttrs: function (index) {
                var that = this;
                if (that.data.attrs.length > 1) {
                        that.data.attrs.splice(index, 1);
                    } else {
                     return   this.$message.error('请设置至少一个规则');
                }
                // this.generates();
            },
            generates: function () {
                var that = this;
                that.generate(1);
            },
            generate: function (type = 0) {
                var that = this;
                axios.post('/merchant/goods_format',{attrs:this.data.items,id:that.data.id,type:type})
                  .then(function (response) {
                      var response_data = response.data;
                      if(response_data.code == 1){
                        that.$set(that.data, 'attrs', response_data.data.value);
                        that.$set(that.data, 'header', response_data.data.attr);
                        that.$set(that, 'formHeader', response_data.data.header);
                      }else{
                          // 错误处理
                          that.$message.error('加载失败: ' + response_data.data.msg);
                          console.error('请求错误:', response_data.data.msg);
                      }
                  })
                  .catch(function (error) {
                      // 错误处理
                      that.$message.error('加载失败: ' + error.message);
                      console.error('请求错误:', error);
                  });
            },
            onGoodsTypeChange(val) {
              if (val === 1) {
                // 选择了实物，清空艺术家选择
                this.data.artist_id = '';
              } else if (val === 2) {
                // 选择了数字珍品，清空分类选择
                this.data.cate_id = '';
              }
            },
            showLayuiAlert() {
                layui.use('layer', function(){
                    var layer = layui.layer;
                    layer.alert('这是从 Vue 调用的 LayUI 弹窗');
                });
            },
            handleimageExceed(){ 
              return  this.$message.error('只能上传一张图片');
             },
            handleimagePreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handleimageRemove(){
                 this.data.image = '';
             },
             handleimageSuccess(response, file, fileList) {
             
                this.data.image= response.data.src;
            },
            
            handleBatchpicSuccess(response, file, fileList) {
            
                this.batchAttr[0].pic= response.data.src;
            },
            handlepicSuccess: function (response, file, fileList) {
               
                this.data.attr[0]['pic'] = response.data.src;
            },
            handleItemSuccess: function (res, file, fileList, index,) {
        
                this.data.attrs[index]['pic'] = res.data.src;
            },
             beforeimageUpload(file) {
               
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG && !isPNG) {
               return this.$message.error("上传头像图片只能是 JPG 、PNG格式!");
                }
                if (!isLt2M) {
              return  this.$message.error("上传头像图片大小不能超过 2MB!");
                }
            
            },
             handleslider_imagePreview(file) {
                this.dialogVisible = true;
                this.previewUrl = file.url;
            },
             handleslider_imageSuccess(response, file, fileList) {
                
                 this.data.slider_image.push(response.data.src);
            },
             beforeslider_imageUpload(file) {
              
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG && !isPNG) {
              return  this.$message.error("上传头像图片只能是 JPG 、PNG格式!");
                }
                if (!isLt2M) {
              return  this.$message.error("上传头像图片大小不能超过 2MB!");
                }
            },
             handleslider_imageRemove(file,fileList) {
                const that = this;
                const fileSrc = file.url
                this.data.slider_imagefileList.forEach(function(element,key) {
                if(element.url === fileSrc){
                that.data.slider_image.splice(key, 1);
                }
                });
            },
            handleRemovevideo_link() {
                  this.data.video_link = "";
                  this.data.video_linkfileList = [];
              },
              back(){
                if(this.activeName == 'first'){
                return
                }else if(this.activeName == 'second'){
                this.activeName = 'first'
                }else if(this.activeName == 'third'){
                this.activeName = 'second'
                }
            },
            next() {
                    if(this.activeName == 'first'){
                    this.activeName = 'second'
                    }else if(this.activeName == 'second'){
                    this.activeName = 'third'
                    }
                },
                handleSubmit:function () {
                    var that = this;
                    if (that.data.cate_id == '' && that.data.goods_type == 1) {
                    return  this.$message.error('请选择商品分类');
                    }
                    if (!that.data.store_name) {
                    return  this.$message.error('请填写商品名称');
                    }
                    if (!that.data.image) {
                    return  this.$message.error('请填选择商品图');
                    }
                    if (!that.data.slider_image.length) {
                    return this.$message.error('请填选择商品轮播图');
                    }
                    if(that.goods_type == 2 && that.data.spec_type ==1){
                    return  this.$message.error('数字珍品不支持多规格');
                    }
                    if (that.data.spec_type == 0) {
                        if (!that.data.attr[0].pic) {
                        return  this.$message.error('请选择单规则图片');
                        }
                        if (that.data.attr[0].price == '') {
                        return  this.$message.error('请输入单规格价格');
                        }
                        if (that.data.attr[0].ot_price == '') {
                        return this.$message.error('请输入单规格原价');
                        }
                        if (!that.data.attr[0].stock) {
                        return this.$message.error('请输入单规格库存');
                        }
                        var attr = {
                            pic:that.data.attr[0].pic,
                            price:that.data.attr[0].price,
                            cost:that.data.attr[0].cost,
                            ot_price:that.data.attr[0].ot_price,
                            stock:that.data.attr[0].stock,
                            bar_code:that.data.attr[0].bar_code,
                        };
                        that.data.attrs = [attr];
                    } else {
                        if (!that.data.attrs.length) {
                            return this.$message.error('请添加多规格属性');
                        }
                        for(var index in that.data.attrs){
                            if(!that.data.attrs[index].pic){
                            return this.$message.error('请选择多规格属性第'+(parseInt(index) +1)+'条的图片');
                            }
                            if(that.data.attrs[index].price === ''){
                            return this.$message.error('请填写多规格属性第'+(parseInt(index) +1)+'条的价格');
                            }
                            if(that.data.attrs[index].ot_price === ''){
                                return this.$message.error('请填写多规格属性第'+(parseInt(index) +1)+'条的原价');
                            }
                            if(that.data.attrs[index].stock === ''){
                            return this.$message.error('请填写多规格属性第'+(parseInt(index) +1)+'条的库存');
                            }
                        }
                    }
                    this.data.description = window.editor.getHtml();
                    axios.post('/merchant/goods_create',this.data)
                        .then(function (response) {
                            var response_data = response.data;
                            if(response_data.code == 200){
                                that.$message({
                                    message: '添加成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose() {
                                        if (that.$refs.form !== undefined) {
                                        that.$refs.form.resetFields()
                                        that.data = {};
                                        that.data.attr = [
                                            {
                                            pic: '',
                                            price: 0,
                                            cost: 0,
                                            ot_price: 0,
                                            stock: 0,
                                            bar_code: ''
                                        },
                                        ]
                                        that.data.attrs = [];
                                        that.data.items = [];
                                        that.activeName = 'first';
                                        }
                                        // that.item.dialogVisible = false
                                    }
                                })
                            }else{
                                // 错误处理
                                that.$message.error('加载失败: ' + response_data.data.msg);
                                console.error('请求错误:', response_data.data.msg);
                            }
                        })
                        .catch(function (error) {
                            // 错误处理
                            that.$message.error('加载失败: ' + error.message);
                            console.error('请求错误:', error);
                        });
                },
             }
        });
</script>
{/block}
